---
title: Conditional Statements
description: Learn how to add content based on conditions in your prompts
---

## Overview

Conditional statements in PromptL enable dynamic and adaptive prompts. By incorporating logic into your prompts, you can:
- Tailor responses based on user input or context.
- Control the flow of conversations dynamically.
- Generate content conditionally for more personalized or complex interactions.

Conditionals are evaluated at runtime, ensuring that your prompts adapt seamlessly to the data provided.

---

## Syntax

Conditional blocks in PromptL use the `if`, `else`, and `endif` keywords, wrapped in `{{ }}`. The content within the block is processed only if the condition evaluates to `true`.

### Basic Syntax
```tsx
{{ if condition }}
  Content to display if the condition is true
{{ else }}
  Content to display if the condition is false
{{ endif }}
```

### Example: Simple Conditional
```tsx
{{ if age < 18 }}
  The user is under the minimum required age. Respond with a kind message explaining this limitation.
{{ else }}
  <user>
    {{ question }}
  </user>
{{ endif }}
```

---

## Advanced Usage

### Checking Variable Existence
You can use conditionals to check if a variable is defined before using it.

```plaintext
{{ if last_name }}
  {{ name = name + " " + last_name }}
{{ endif }}

<user>
  Hi! My name is {{ name }}.
</user>
```

### Nested Conditions
Conditionals can be nested to handle more complex logic.

```tsx
{{ if role == "admin" }}
  <system>
    The user is an admin, and has full access to all data.
  </system>
  {{ if feature_enabled }}
    In addition, the special feature is enabled for this user.
  {{ endif }}
{{ else }}
  <system>
    The user has a standard role and does not have access to admin features.
  </system>
{{ endif }}
```

### Using Expressions in Conditions
Conditions can include complex expressions, such as combining variables or performing calculations.

```tex
{{ if items_in_cart > 0 && user_logged_in }}
  <assistant>
    You have {{ items_in_cart }} items in your cart. Ready to checkout?
  </assistant>
{{ else }}
  <assistant>
    Your cart is empty. Start shopping to add items!
  </assistant>
{{ endif }}
```

---

## Best Practices

1. **Keep It Simple**: Avoid deeply nested conditionals. Break complex logic into smaller, reusable components.
2. **Define Default Values**: Ensure variables have defaults (`||`) to prevent unexpected errors.
3. **Test Edge Cases**: Check how your logic handles undefined variables or null values.
4. **Use Readable Conditions**: Use descriptive variable names and straightforward logic to improve maintainability.
   - ✅ Good: `{{ if user_logged_in && has_permission }}`
   - ❌ Bad: `{{ if x > 0 || y == 1 }}`

---

## Debugging Conditionals

If your conditional logic isn't behaving as expected:
- **Verify Variable Values**: Check if the variables used in your condition are defined and contain the expected data.
- **Simplify Conditions**: Break down complex expressions into smaller, testable conditions.
- **Add Debug Statements**: Temporarily output variable values for troubleshooting:
  ```tsx
    Debug: {{ user_logged_in && has_permission }}
  ```

---

## Advanced Example

Here’s a real-world example of a conditional block for a personalized travel assistant:

```tsx
<system>
  You are a travel assistant.
</system>

{{ if destination }}
  <user>
    Show me popular attractions in {{ destination }}.
  </user>
{{ else }}
  <user>
    I’d like some travel recommendations.
  </user>
{{ endif }}
```

---

## Summary

Conditional statements are a powerful tool in PromptL, enabling dynamic and personalized prompts that adapt to user input and context. By combining them with variables and expressions, you can build highly responsive and flexible conversations.

> Next: Learn about [Loops and Iteration](/promptl/syntax/loops) for even more dynamic capabilities.
